package common.utils;

import android.content.Context;

import com.enqualcomm.kids.R;

import java.util.Calendar;

public class TimeUtil {
    /**
     * 获取从给定时间到当前时间的差值的描述文字
     * @param time
     * @return
     */
    public static String getStayTime(long time,Context context){
        int year = (int)(time/1E10);
        int month = (int)(time/1E8)-year*100;
        int day = (int)(time/1E6) -year*10000-month*100;
        int hour = (int)(time/1E4)-year*1000000-month*10000-day*100;
        int minute = (int)(time/1E2-year*1E8-month*1E6-day*1E4-hour*1E2);
        int second = (int)(time - year*1E10-month*1E8-day*1E6-hour*1E4 - minute*1E2);

//		MyLogger.jLog().i("year:"+year+" month:"+month+" day:"+day+" hour:"+hour+" minute:"+minute+" second:"+second);
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day, hour, minute, second);
        long stayMillis = System.currentTimeMillis()-calendar.getTimeInMillis();//停留的毫秒值
//		MyLogger.jLog().i("停留的毫秒值："+stayMillis);
        StringBuilder builder = new StringBuilder();
        long stayDay = stayMillis/1000/3600/24;
        if (stayDay>=1) {
            stayMillis -= 1000*3600*24*stayDay;
            builder.append(stayDay).append(context.getString(R.string.day2));
        }
        long stayHour = stayMillis/1000/3600;
        if (stayHour>=1) {
            stayMillis -= 1000*3600*stayHour;
            builder.append(stayHour).append(context.getString(R.string.hour2));
        }
//		else if (condition) {
//			
//		}
        builder.append(stayMillis/1000/60).append(context.getString(R.string.minute));

        return builder.toString();
    }

    public static int getNextDayOfWeek(){
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                week = 2;
                break;
            case 2:
                week = 4;
                break;
            case 3:
                week = 8;
                break;
            case 4:
                week = 16;
                break;
            case 5:
                week = 32;
                break;
            case 6:
                week = 64;
                break;
            case 7:
                week = 1;
                break;

            default:
                break;
        }
        return week;
    }

    public  static String[] getTodayStartAndEndTime(){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return getTodayStartAndEndTime(year,month,day);
    }

    public static String[] getTodayStartAndEndTime(int year, int month, int day){
        String strmonth,strday;
        if (month < 10) {
            strmonth = "0" + month;
        } else {
            strmonth = "" + month;
        }
        if (day < 10) {
            strday = "0" + day;
        } else {
            strday = "" + day;
        }

        String[] strings = new String[2];
        strings[0] = year + strmonth + strday + "000000";//开始时间 20141001000000 ,发送给服务器的
        strings[1] = year + strmonth + strday + "235959";
        return strings;
    }

    public static String millisToFormatStr(long millis){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(year);
        if (month<10){
            stringBuilder.append("0").append(month);
        }else {
            stringBuilder.append(month);
        }
        if (day<10){
            stringBuilder.append("0").append(day);
        }else {
            stringBuilder.append(day);
        }
        if (hour<10){
            stringBuilder.append("0").append(hour);
        }else {
            stringBuilder.append(hour);
        }
        if (minute<10){
            stringBuilder.append("0").append(minute);
        }else {
            stringBuilder.append(minute);
        }
        if (second<10){
            stringBuilder.append("0").append(second);
        }else {
            stringBuilder.append(second);
        }

        return stringBuilder.toString();
    }

}
